package br.edu.ufcg.greengrid.workload;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class PlainTextWorkloadParser extends AbstractWorkloadParser {

	private PlainTextConverter converter;
	
	public PlainTextWorkloadParser(PlainTextConverter converter) {
		
		super();
		this.converter = converter;
	}

	public void parse(List<String> filesName) throws IOException {

		List<File> inputs = getInputFiles(filesName);

		String line = null;
		BufferedReader br = null;

		for (File input : inputs) {
			try {
				br = new BufferedReader(new FileReader(input));

				while ((line = br.readLine()) != null) {
					this.converter.convert(input.getName(), line);
				}

			} finally {
				if (br != null) {
					br.close();
				}
			}
			this.converter.close();
		}
	}

	public void generateOutput(File outputFile) throws IOException {
		this.generateOutput(outputFile, this.converter.getLines());
	}

}
